﻿using Parylene_CVD.Commands;
using Parylene_CVD.DAL;
using Parylene_CVD.Model;
using Parylene_CVD.Model.DModels;
using Parylene_CVD.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;



namespace Parylene_CVD.ViewModel
{
    public class SetViewModel : ViewModelBase
    {
        public SetViewModel(NavigationService HomeViewNavigationService, NavigationService UserManagementNavigationService, NavigationService DeviceManagementNavigationService) {
            SetSavePath();
            _userManagementNavigationService = UserManagementNavigationService;
            UserManagementCommand = new RelayCommand(UserManagement);
            DeviceManagementCommand = new NavigateCommand(DeviceManagementNavigationService);
            ExitCommand = new NavigateCommand(HomeViewNavigationService);
            SelectSavePathCommand = new RelayCommand(SelectSavePath);
        }

        private readonly NavigationService _userManagementNavigationService;
        public ICommand UserManagementCommand { get; set; }
        public void UserManagement(object parameter)
        {
            if(GlobalVariables.LoginUserRole == GlobalVariables.AdminTitle)
            {
                _userManagementNavigationService.Navigate();
            }
            else
            {
                MessageBox.Show("对不起，您没有权限进入", "权限不足", MessageBoxButton.OK, MessageBoxImage.Warning);

            }
        }

        public ICommand DeviceManagementCommand { get; set; }

        public ICommand ExitCommand { get; set; }
        public ICommand SelectSavePathCommand { get; set; }
        // 打开文件夹选择对话框
        private async void SelectSavePath(object parameter)
        {
            var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
            if (dialog.ShowDialog() == true)
            {
                SavePath = dialog.SelectedPath;
                parameter_savepath.ParameterValue = SavePath;
                parameterDAL.UpdataParameter(parameter_savepath, "ParameterValue");
            }
        }


        private string _savePath;
        public string SavePath
        {
            get { return _savePath; }
            set
            {
                _savePath = value;
                OnPropertyChanged(nameof(SavePath));
            }
        }
        ParameterDAL parameterDAL = new ParameterDAL();
        public Parameter parameter_savepath { get; set; }
        public void SetSavePath()
        {
            parameter_savepath = parameterDAL.GetParameter("ParameterName = '数据保存目录'", "")[0];
            SavePath = parameter_savepath.ParameterValue;
        }
    }
}
